import { useNavigate, useParams } from 'react-router-dom';
import { useEffect } from 'react';
import { Button, Card, Form, Input ,message} from 'antd';

import { addHospitalRequest,getHospitalFromIdRequest,updateHosFromIdRequest } from '@/api/hospital/hospitalSet';


import type { addHospitalParamsType } from "@api/hospital/model/hospitalSetType"


export default function AddHospital() {
  //编程式路由导航
  const navigate = useNavigate()
  //获取更新跳转携带的id值
  const {id} = useParams()
  //创建表单实例
  const [form] = Form.useForm();



  //获取更新数据对应的数据
  useEffect(() => {
      if (id) {
        const getHosFromId = async () => {
        const result = await getHospitalFromIdRequest(id)
        //填充表单
        form.setFieldsValue(result)
        
      }
      getHosFromId()
    }
  }, [])



  //添加Or更新医院数据的回调
  const addHospitalHandle = async (infinish: addHospitalParamsType) => {
    if(id){
      //更新医院数据的回调
     await updateHosFromIdRequest({id,...infinish})
      //提交后跳转到医院设置
    navigate("/syt/hospital/hospitalSet")
    message.success('更新成功')
    return 
    }
    //添加医院数据的回调
    await addHospitalRequest(infinish)
    //提交后跳转到医院设置
    navigate("/syt/hospital/hospitalSet")
    message.success('添加成功')
  }

  return (
    <Card>
      <Form
        form={form}
        name="basic"
        labelCol={{ span: 3, }}
        wrapperCol={{ span: 18, }}
        initialValues={{ remember: true, }}
        onFinish={addHospitalHandle}
      >
        <Form.Item
          label="医院名称"
          name="hosname"
          rules={[
            {
              required: true,
              message: '请输入',
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="医院编号"
          name="hoscode"
          rules={[
            {
              required: true,
              message: '请输入',
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="api基础路径"
          name="apiUrl"
          rules={[
            {
              required: true,
              message: '请输入',
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="联系人姓名"
          name="contactsName"
          rules={[
            {
              required: true,
              message: '请输入',
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="联系人手机"
          name="contactsPhone"
          rules={[
            {
              required: true,
              message: '请输入',
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          wrapperCol={{
            offset: 3,
            span: 16,
          }}
        >
          <Button type="primary" htmlType="submit">
            提交
          </Button>
          <Button type="default" onClick={() => { navigate(-1) }} >
            返回
          </Button>
        </Form.Item>
      </Form>
    </Card>
  )
}



